package com.aegean.MonitorExamSYS.center.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.opensymphony.xwork2.ActionSupport;

@Controller("ShowMonitorListForMonitor")
@Scope("prototype")
public class ShowMonitorListForMonitor extends ActionSupport implements
		ServletRequestAware {
	@Resource
	CenterBusiness business;
	private HttpServletRequest request;
	private HttpSession session;
	private List<MonitorValue> monitors;
	private int total;
	private Date startTime;
	private Date endTime;
	private int unit_Id;
	private int big_Id;
	private Map<Integer, List<MonitorValue>> monitorMap = null;
    private List<MonitorValue> selectMonitor;
    
	public List<MonitorValue> getSelectMonitor() {
		return selectMonitor;
	}

	public void setSelectMonitor(List<MonitorValue> selectMonitor) {
		this.selectMonitor = selectMonitor;
	}

	public int getBig_Id() {
		return big_Id;
	}

	public void setBig_Id(int big_Id) {
		this.big_Id = big_Id;
	}

	public List<MonitorValue> getMonitors() {
		return monitors;
	}

	public void setMonitors(List<MonitorValue> monitors) {
		this.monitors = monitors;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public int getUnit_Id() {
		return unit_Id;
	}

	public void setUnit_Id(int unit_Id) {
		this.unit_Id = unit_Id;
	}

	@Override
	public String execute() throws Exception {
		monitors=new ArrayList<MonitorValue>();
		// TODO Auto-generated method stub
		if (session.getAttribute("MonitorsForMonitorApply") == null) {
			//System.out.print("------------4");
			monitors.addAll(business.showMonitorsListForMonitor(unit_Id, startTime,
					endTime));
			monitorMap = new HashMap<Integer, List<MonitorValue>>();
			monitorMap.put(unit_Id, monitors);
			session.setAttribute("MonitorsForMonitorApply", monitorMap);// 定义保存Monitors;
		} else {
			//System.out.print("------------3");
			monitorMap = (Map<Integer, List<MonitorValue>>) session
					.getAttribute("MonitorsForMonitorApply");
			if (monitorMap.containsKey(unit_Id)) {
				//System.out.print("------------1");
				monitors = monitorMap.get(unit_Id);
			} else {
				//System.out.print("---------2");
				monitors.addAll(business.showMonitorsListForMonitor(unit_Id, startTime,
						endTime));
				if (!monitors.isEmpty()) {
					monitorMap = new HashMap<Integer, List<MonitorValue>>();
					monitorMap.put(unit_Id, monitors);
					session.setAttribute("MonitorsForMonitorApply", monitorMap);// 定义保存Monitors;
				}
			}
		}
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
		this.session = request.getSession();

	}

}
